package com.beta.service.impl;

import com.beta.constant.CommonConstant;
import com.beta.entity.OrderItems;
import com.beta.service.IPaymentsService;
import com.beta.em.order.OrderItemStatus;
import com.beta.em.order.RefundStatus;
import com.beta.service.IRefundItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Created by vector on 2016/12/18.
 */
@Service
public class RefundItemService implements IRefundItemService {

    @Autowired
    private IPaymentsService paymentsService;

    @Override
    public BigDecimal getRefundPayAmount(OrderItems orderItems) {
        if (!(OrderItemStatus.REJECTED.getValue().equals(orderItems.getStatus())
                && RefundStatus.AGREED.getValue().equals(orderItems.getRefundStatus())
                && Objects.nonNull(orderItems.getRefundRate()))) {
            return BigDecimal.ZERO;
        }

        BigDecimal payFeeRatio = paymentsService.getPaymentFeeRatioByOrderId(orderItems.getOrderId());
        return orderItems.getPrice()
                .multiply(BigDecimal.ONE.subtract(payFeeRatio))
                .multiply(BigDecimal.ONE.subtract(orderItems.getRefundRate()))
                .setScale(2, CommonConstant.ROUNDING_MODE);
    }

}
